//
// Created by 86150 on 2021/10/9.
//
#ifndef BJUT_CPP_2_LOOKERS_H
#define BJUT_CPP_2_LOOKERS_H

class looker: public QWidget
{
	private:
		std::ifstream raw_txt;
		std::string buffer_;
		std::istringstream buffer;
		common_employee employees[4];
		std::string temp_id, temp_name, kind;
		int hours, soild_salary, sale_amount;
		Ui::Form my_ui;
		DB_worker sqlboy;

		void ui_insert()
		{
			common_employee temp(my_ui.lineEdit_2->text().toStdString(),
								 my_ui.lineEdit_3->text().toStdString(),
								 my_ui.lineEdit->text().toStdString(),
								 std::stoi(my_ui.lineEdit_7->text().toStdString()),
								 std::stoi(my_ui.lineEdit_12->text().toStdString()),
								 std::stoi(my_ui.lineEdit_13->text().toStdString()));
			sqlboy.insert(temp);
			my_ui.textBrowser->setText(QString::fromStdString(sqlboy.stringlize(temp)));
		}
		void ui_remove()
		{
			sqlboy.remove(my_ui.lineEdit_4->text().toStdString());
			my_ui.textBrowser->setText(QString::fromStdString(sqlboy.stringlize(sqlboy.get(my_ui.lineEdit_4->text().toStdString()))));
		}
		void ui_find()
		{
			my_ui.textBrowser->setText(QString::fromStdString(sqlboy.stringlize(sqlboy.get(my_ui.lineEdit_5->text().toStdString()))));
		}
		void ui_change()
		{
			common_employee temp(my_ui.lineEdit_10->text().toStdString(),
								 my_ui.lineEdit_9->text().toStdString(),
								 my_ui.lineEdit_11->text().toStdString(),
								 std::stoi(my_ui.lineEdit_8->text().toStdString()),
								 std::stoi(my_ui.lineEdit_14->text().toStdString()),
								 std::stoi(my_ui.lineEdit_15->text().toStdString()));
			sqlboy.remove(my_ui.lineEdit_10->text().toStdString());
			sqlboy.insert(temp);
			my_ui.textBrowser->setText(QString::fromStdString(sqlboy.stringlize(temp)));
		}

	public:
		looker(std::string filename)
		{
			raw_txt.open(filename);
			int j = 0;

			while (getline(raw_txt, buffer_)) {
				buffer.str(buffer_);
				buffer>>temp_id>>temp_name>>kind>>hours>>soild_salary>>sale_amount;
				common_employee temp(temp_id, temp_name, kind, hours, soild_salary, sale_amount);
				employees[j++] = temp;
				buffer.clear();
			}
			for (int i = 0; i < 4; i++) {
				sqlboy.insert(employees[i]);
			}

			my_ui.setupUi(&my_ui);
			QObject::connect(my_ui.pushButton, &QPushButton::clicked, this, &looker::ui_insert);
			QObject::connect(my_ui.pushButton_2, &QPushButton::clicked, this, &looker::ui_remove);
			QObject::connect(my_ui.pushButton_3, &QPushButton::clicked, this, &looker::ui_find);
			QObject::connect(my_ui.pushButton_4, &QPushButton::clicked, this, &looker::ui_change);
			QMetaObject::connectSlotsByName(this);
		}
		void in_terminal()
		{
			for (auto x: employees) std::cout<<sqlboy.stringlize(sqlboy.get(x.get_id()))<<std::endl;
		}
		void in_GUI()
		{
			my_ui.show();
		}

};
#endif //BJUT_CPP_2_LOOKERS_H
